package com.elpebe.sysabk.web;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.elpebe.sysabk.model.Account;
import com.elpebe.sysabk.service.IAccountService;
import com.elpebe.sysabk.web.response.JsonResponseFactory;
import com.elpebe.sysabk.web.response.JsonResponseFactory.Response;

@Controller
@RequestMapping("accounts")
public class AccountController extends AbstractCrudController<Account>{
	
	@Inject IAccountService mService;

	@Override
	protected Response getCreateResponse(Account account) {
		JsonResponseFactory.Response response;
		
		try{
			account = mService.create(account);
			response  = JsonResponseFactory.createSuccessResponse(account);

		} catch (Exception e) {
			response = JsonResponseFactory.createErrorResponse(e.getMessage());
		}
		
		return response;
	}

	@Override
	protected Response getReadResponse(int page, int start, int limit) {
		JsonResponseFactory.Response response;
		try{

			List<Account> accounts = mService.findAll();
			response  = JsonResponseFactory.createSuccessResponse(accounts);

		} catch (Exception e) {
			response = JsonResponseFactory.createErrorResponse(e.getMessage());
		}
		
		return response;
	}

	@Override
	protected Response getUpdateResponse(Account account) {
		JsonResponseFactory.Response response;
		
		try{			
			account = mService.update(account);
			response  = JsonResponseFactory.createSuccessResponse(account);

		} catch (Exception e) {
			response = JsonResponseFactory.createErrorResponse(e.getMessage());
		}
		
		return response;
	}

	@Override
	protected Response getDeleteResponse(Long id) {
		JsonResponseFactory.Response response;
		
		try{

			mService.delete(id);
			response  = JsonResponseFactory.createSuccessResponse("Successfully deleted");

		} catch (Exception e) {
			response = JsonResponseFactory.createErrorResponse(e.getMessage());
		}
		
		return response;
	}	
	
}
